<template>
  <view class="uni-container">
    <uni-forms ref="form" :model="formData" validateTrigger="bind">
      <uni-forms-item name="nickname" label="昵称">
        <uni-easyinput placeholder="用户昵称" v-model="formData.nickname" trim="both"></uni-easyinput>
      </uni-forms-item>
      <uni-forms-item name="name" label="姓名">
        <uni-easyinput placeholder="姓名" v-model="formData.name" trim="both"></uni-easyinput>
      </uni-forms-item>
      <uni-forms-item name="phone" label="手机号码">
        <uni-easyinput placeholder="手机号码" v-model="formData.phone" trim="both"></uni-easyinput>
      </uni-forms-item>
      <uni-forms-item name="identity" label="身份证号码">
        <uni-easyinput placeholder="身份证号码" v-model="formData.identity" trim="both"></uni-easyinput>
      </uni-forms-item>
      <uni-forms-item name="birthday" label="出生年月日">
        <uni-easyinput placeholder="出生年月日" v-model="formData.birthday" trim="both"></uni-easyinput>
      </uni-forms-item>
      <uni-forms-item name="school" label="毕业院校">
        <uni-easyinput placeholder="毕业院校" v-model="formData.school" trim="both"></uni-easyinput>
      </uni-forms-item>
      <uni-forms-item name="degree" label="学历">
        <uni-easyinput placeholder="学历 大专/本科/研究生" v-model="formData.degree" trim="both"></uni-easyinput>
      </uni-forms-item>
      <uni-forms-item name="major" label="专业">
        <uni-easyinput placeholder="专业" v-model="formData.major" trim="both"></uni-easyinput>
      </uni-forms-item>
      <uni-forms-item name="job_title" label="职称">
        <uni-easyinput placeholder="职称" v-model="formData.job_title" trim="both"></uni-easyinput>
      </uni-forms-item>
      <uni-forms-item name="workplace" label="工作单位">
        <uni-easyinput placeholder="工作单位" v-model="formData.workplace" trim="both"></uni-easyinput>
      </uni-forms-item>
      <uni-forms-item name="home_address" label="家庭住址">
        <uni-easyinput placeholder="家庭住址" v-model="formData.home_address" trim="both"></uni-easyinput>
      </uni-forms-item>
      <uni-forms-item name="training" label="参加过哪些培训">
        <uni-easyinput placeholder="参加过哪些培训" v-model="formData.training" trim="both"></uni-easyinput>
      </uni-forms-item>
      <uni-forms-item name="degree_provement" label="学历证明">
				<!-- <uni-data-checkbox :multiple="true" v-model="formData.degree_provement"></uni-data-checkbox> -->
				<!-- <uni-easyinput placeholder="学历证明" v-model="formData.degree_provement" trim="both"></uni-easyinput> -->
				<uni-file-picker v-model="formData.degree_provement" fileMediatype="image" mode="grid"
					@select="i_select" @progress="i_progress" @success="i_success" @fail="i_fail" />
      </uni-forms-item>
      <uni-forms-item name="job_title_provement" label="职称证明">
				<uni-file-picker v-model="formData.job_title_provement" fileMediatype="image" mode="grid"
					@select="i_select" @progress="i_progress" @success="i_success" @fail="i_fail" />
      </uni-forms-item>
      <uni-forms-item name="training_provement" label="培训证书">
				<uni-file-picker v-model="formData.training_provement" fileMediatype="image" mode="grid"
					@select="i_select" @progress="i_progress" @success="i_success" @fail="i_fail" />
      </uni-forms-item>
      <uni-forms-item name="resume" label="简历">
        <uni-easyinput placeholder="工作简历" v-model="formData.resume" trim="both"></uni-easyinput>
      </uni-forms-item>
      <uni-forms-item name="skilled" label="擅长领域">
				<uni-data-checkbox :multiple="true" v-model="formData.skilled"
					:localdata="formOptions.skilled_localdata"></uni-data-checkbox>
      </uni-forms-item>
      <uni-forms-item name="talent" label="评估后胜任领域">
        <uni-data-checkbox :multiple="true" v-model="formData.talent" :localdata="formOptions.talent_localdata"></uni-data-checkbox>
      </uni-forms-item>
      <uni-forms-item name="reason" label="审核原因">
        <uni-easyinput placeholder="审核原因" v-model="formData.reason" trim="both"></uni-easyinput>
      </uni-forms-item>
      <uni-forms-item name="status" label="审核状态">
        <uni-data-checkbox v-model="formData.status" :localdata="formOptions.status_localdata"></uni-data-checkbox>
      </uni-forms-item>
      <view class="uni-button-group">
        <button type="primary" class="uni-button" style="width: 100px;" @click="submit">提交</button>
        <navigator open-type="navigateBack" style="margin-left: 15px;">
          <button class="uni-button" style="width: 100px;">返回</button>
        </navigator>
      </view>
    </uni-forms>
  </view>
</template>

<script>
  import { validator } from '../../js_sdk/validator/mer-physios.js';

  const db = uniCloud.database();
  const dbCmd = db.command;
  const dbCollectionName = 'mer-physios';

  function getValidator(fields) {
    let result = {}
    for (let key in validator) {
      if (fields.includes(key)) {
        result[key] = validator[key]
      }
    }
    return result
  }

  

  export default {
    data() {
      let formData = {
        "nickname": "",
        "name": "",
        "phone": "",
        "identity": "",
        "birthday": "",
        "school": "",
        "degree": "",
        "major": "",
        "job_title": "",
        "workplace": "",
        "home_address": "",
        "training": "",
        "degree_provement": [],
        "job_title_provement": [],
        "training_provement": [],
        "resume": "",
        "skilled": [],
        "talent": [],
        "reason": "",
        "status": 1
      }
      return {
        formData,
        formOptions: {
          "skilled_localdata": [
            {
              "text": "青少年特发性脊柱侧弯康复",
              "value": "1"
            },
            {
              "text": "各类不良体态矫正",
              "value": "2"
            },
            {
              "text": "产后康复",
              "value": "3"
            }
          ],
          "talent_localdata": [
            {
              "text": "体态评估",
              "value": "65f9ada021821b6d2b3816ce"
            },
            {
              "text": "脖子前倾/富贵包",
              "value": "65f9adae9755e32830ebc469"
            },
            {
              "text": "溜肩",
              "value": "65f9adb5a09a9b12d71ff0c0"
            },
            {
              "text": "圆肩驼背",
              "value": "65f9adbb09664cbba09a40a4"
            },
            {
              "text": "高低肩",
              "value": "65f9adc121821b6d2b381c2c"
            },
            {
              "text": "肋骨外翻",
              "value": "65f9adc655b3372a1fa5ffdf"
            },
            {
              "text": "脊柱侧弯",
              "value": "65f9adcca09a9b12d71ff4c2"
            },
            {
              "text": "骨盆前倾",
              "value": "65f9add3f08210b07d29dbab"
            },
            {
              "text": "假胯宽",
              "value": "65f9addae0ec199b181e2f4a"
            },
            {
              "text": "产后骨盆修复",
              "value": "65f9ade2213929f86662c351"
            },
            {
              "text": "SPS训练团课",
              "value": "65f9ade9189f861ff7aba6b0"
            },
            {
              "text": "体态塑形体验",
              "value": "65f9adf109664cbba09a4926"
            },
            {
              "text": "局部舒缓-头颈肩",
              "value": "65f9adfa189f861ff7abaf9c"
            },
            {
              "text": "局部舒缓-腰背腿",
              "value": "65f9ae019755e32830ebd285"
            },
            {
              "text": "扁平足",
              "value": "65f9ae0799c6244dcfd658d2"
            }
          ],
          "status_localdata": [
            {
              "text": "禁用",
              "value": 0
            },
            {
              "text": "审核中",
              "value": 1
            },
            {
              "text": "审核通过",
              "value": 2
            },
            {
              "text": "审核拒绝",
              "value": 3
            }
          ]
        },
        rules: {
          ...getValidator(Object.keys(formData))
        }
      }
    },
    onLoad(e) {
      if (e.id) {
        const id = e.id
        this.formDataId = id
        this.getDetail(id)
      }
    },
    onReady() {
      this.$refs.form.setRules(this.rules)
    },
    methods: {
      
      /**
       * 验证表单并提交
       */
      submit() {
        uni.showLoading({
          mask: true
        })
        this.$refs.form.validate().then((res) => {
          return this.submitForm(res)
        }).catch(() => {
        }).finally(() => {
          uni.hideLoading()
        })
      },

      /**
       * 提交表单
       */
      submitForm(value) {
        // 使用 clientDB 提交数据
        return db.collection(dbCollectionName).doc(this.formDataId).update(value).then((res) => {
          uni.showToast({
            title: '修改成功'
          })
          this.getOpenerEventChannel().emit('refreshData')
          setTimeout(() => uni.navigateBack(), 500)
        }).catch((err) => {
          uni.showModal({
            content: err.message || '请求服务失败',
            showCancel: false
          })
        })
      },

      /**
       * 获取表单数据
       * @param {Object} id
       */
      getDetail(id) {
        uni.showLoading({
          mask: true
        })
        db.collection(dbCollectionName).doc(id).field("nickname,name,phone,identity,birthday,school,degree,major,job_title,workplace,home_address,training,degree_provement,job_title_provement,training_provement,resume,skilled,talent,reason,status").get().then((res) => {
          const data = res.result.data[0]
          if (data) {
            this.formData = data
            
          }
        }).catch((err) => {
          uni.showModal({
            content: err.message || '请求服务失败',
            showCancel: false
          })
        }).finally(() => {
          uni.hideLoading()
        })
		},

		// 获取上传状态
		i_select(e) {
			console.log('选择文件：', e)
		},
		// 获取上传进度
		i_progress(e) {
			console.log('上传进度：', e)
		},

		// 上传成功
		i_success(e) {
			console.log('上传成功')
		},

		// 上传失败
		i_fail(e) {
			console.log('上传失败：', e)
		}
    }
  }
</script>
